package com.ruoyi.web.controller.app;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PaOrder;
import com.ruoyi.system.domain.bo.SystemOrderRequestBo;
import com.ruoyi.system.domain.bo.UserOrderBo;
import com.ruoyi.system.domain.vo.UserOrderVo;
import com.ruoyi.system.service.IUserOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName UserOrderController
 * @Description app 用户订单相关控制器
 * @date 2024/7/3 8:22
 * @Version 1.0
 * @Auther: zhangzhenyu
 */
@RestController
@RequestMapping("/user/order")
@Api(value = "用户代购下单服务", tags = "用户代购下单服务")
public class UserOrderController  extends BaseController {


    @Autowired
    private IUserOrderService userOrderService;
    /**
     * 新增代购订单（purchase agency）
     */
    @Log(title = "用户提交代购订单（purchase agency）", businessType = BusinessType.INSERT)
    @ApiOperation(value = "用户提交代购订单", notes = "用户提交代购订单")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public R add(@Validated @RequestBody SystemOrderRequestBo userOrderBo)
    {
        userOrderBo.setUserId(getUserId());
        userOrderBo.setCreateBy(String.valueOf(getUserId()));
        return R.ok(userOrderService.insertPaOrder(userOrderBo));
    }

    @ApiOperation(value = "用户查询代购订单列表", notes = "用户查询代购订单列表")
    @Log(title = "用户查询代购订单列表（purchase agency）", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public TableDataInfo<UserOrderVo> list(UserOrderBo userOrderBo)
    {
        userOrderBo.setUserId(getUserId());
        userOrderBo.setSupplierId(getUserId());
        return userOrderService.selectPaOrderList(userOrderBo);
    }

    /**
     * 获取代购订单（purchase agency）详细信息
     */
    @ApiOperation(value = "供应商用户获取订单详情", notes = "用户获取订单详情")
    @Log(title = "供应商用户获取订单详情", businessType = BusinessType.OTHER)
    @GetMapping(value = "/{orderId}")
    public R<UserOrderVo> getInfo(@PathVariable("orderId") Long orderId)
    {
        Long userId = getUserId();
        return R.ok(userOrderService.selectPaOrderByOrderId(orderId, userId));
    }

    /**
     * 修改代购订单（purchase agency）
     */
    @ApiOperation(value = "代购订单编辑", notes = "代购订单编辑")
    @Log(title = "代购订单编辑（purchase agency）", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public R edit(@RequestBody SystemOrderRequestBo userOrderBo)
    {
        //获取订单信息
        PaOrder paOrder = userOrderService.selectByOrderId(userOrderBo.getOrderId());
        if (paOrder == null) {
            return R.fail("订单不存在");
        }

        userOrderBo.setUpdateBy(String.valueOf(getUserId()));
        return R.ok(userOrderService.updatePaOrder(userOrderBo));
    }

}
